/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.cmd;

import java.math.BigInteger;

import net.sf.cindy.Buffer;
import net.sf.cindy.buffer.BufferFactory;
import cn.tworen.demou.rtmp.ByteUtil;
import cn.tworen.demou.rtmp.Client;

/**
 * Created on  2007-1-17
 *
 * Title       : AbsMessage.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-1-17
 * History     : 
 * 
 */
public abstract class AbsCmd implements ICmd {
	private Header header = new Header();
	
	public void fireSend(Client client) {
		client.getRoutePacketOut().route(this);
	}
	public Buffer genHeaderBuffer(int bodyLenght, int splite){
		return genHeaderBuffer(
				this.header.getHeaderLenght(), 
				this.header.getSource(),
				bodyLenght,
				this.header.getDataType().value,
				this.header.getTimer(),
				splite
				);
	}
	public Buffer genHeaderBuffer(int headerLenght, int[] source, int bodyLenght, byte dataType, byte[] timer, int splite) {
		Buffer headerbuffer = BufferFactory.allocate(headerLenght);
		switch(headerLenght) {
		case 12:
			headerbuffer.put(8, (byte)source[0]);
			headerbuffer.put(9, (byte)source[1]);
			headerbuffer.put(10, (byte)source[2]);
			headerbuffer.put(11, (byte)source[3]);
		case 8:
			headerbuffer.put(7, dataType);
			//lenght
			BigInteger big = new BigInteger("" + bodyLenght, 10);
			String bigHex = big.toString(16);
			if (bigHex.length() <= 2) {
				headerbuffer.put(4, (byte)0x00);
				headerbuffer.put(5, (byte)0x00);
				headerbuffer.put(6, (byte)bodyLenght);
			} else if (bigHex.length() <= 4) {
				headerbuffer.put(4, (byte)0x00);
				if (bigHex.length() < 4) {
					BigInteger big2 = new BigInteger(bigHex.substring(0, 1), 16);
					headerbuffer.put(5, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(1, 3), 16);
					headerbuffer.put(6, (byte)big2.intValue());
				} else {
					BigInteger big2 = new BigInteger(bigHex.substring(0, 2), 16);
					headerbuffer.put(5, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(2, 4), 16);
					headerbuffer.put(6, (byte)big2.intValue());
				}
			} else{
				if (bigHex.length() < 5) {
					BigInteger big2 = new BigInteger(bigHex.substring(0, 1), 16);
					headerbuffer.put(4, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(1, 3), 16);
					headerbuffer.put(5, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(3, 5), 16);
					headerbuffer.put(6, (byte)big2.intValue());
				} else {
					BigInteger big2 = new BigInteger(bigHex.substring(0, 2), 16);
					headerbuffer.put(4, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(2, 4), 16);
					headerbuffer.put(5, (byte)big2.intValue());
					big2 = new BigInteger(bigHex.substring(4, 6), 16);
					headerbuffer.put(6, (byte)big2.intValue());
				}
			}
		case 4:
			headerbuffer.put(1, timer);
		case 1:
			headerbuffer.put(0, ByteUtil.getHeaderFlag(splite, headerLenght));
		}
		return headerbuffer;
		
	}

	/**
	 * @hibernate.property
	 * @return Returns the header.
	 */
	public Header getHeader() {
		return this.header;
	}

	/**
	 * @param header the header to set
	 */
	public void setHeader(Header header) {
		this.header = header;
	}
	public void release() {
		// do nothing
	}
}
